﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{

	public class UsergroupsDAO:IData
 	{
		public UsergroupsDAO(String myconnecString) : base(myconnecString) { }

		public  int OnSubmit(Usergroups item, int t) {
			 return connec.ExecuteNonQuery("sp_Usergroups", CommandType.StoredProcedure,new SqlParameter("@flag", t),
				 new SqlParameter("@" + Usergroups.c_id, item.id), new SqlParameter("@" + Usergroups.c_groupName, item.groupName),
				 new SqlParameter("@" + Usergroups.c_description, item.description), new SqlParameter("@" + Usergroups.c_isActive, item.isActive)); 
		}

		public  Usergroups GetItem(IDataReader dr)
		{
			Usergroups obj = new Usergroups();
			obj.id = Convert.ToInt32(dr[Usergroups.c_id]);
			obj.groupName = Convert.ToString(dr[Usergroups.c_groupName]);
			obj.description = Convert.ToString(dr[Usergroups.c_description]);
			obj.isActive = Convert.ToInt32(dr[Usergroups.c_isActive]);
			return obj;
		}

        private List<Usergroups> GetAll(String strSQL, CommandType commandtype)
        {
            List<Usergroups> list = new List<Usergroups>();
            SqlConnection connection = connec.Connection;
            SqlCommand command = new SqlCommand();
            command.CommandType = commandtype;
            command.CommandText = strSQL;
            command.Connection = connection;
            connection.Open();
            SqlDataReader reader;
            using (reader = command.ExecuteReader(CommandBehavior.CloseConnection))
            {
                while (reader.Read())
                {
                    list.Insert(0, GetItem(reader));
                }
                reader.Close();
            }
            connection.Close();
            return list;
        }
		public  List<Usergroups> GetAll()
		{
			return GetAll("select * from " + Usergroups.b_Usergroups, CommandType.Text);
		}

		public  Usergroups GetItemById(object id)
		{
			 return this.GetAll().FirstOrDefault(x => x.id == (long)id);
		}

	}

 }
